package com.example.mallproduct.controller;

import com.common.utils.PageUtils;
import com.common.utils.R;
import com.example.mallproduct.controller.vo.AttrGroupWithAttrsVo;
import com.example.mallproduct.entity.AttrEntity;
import com.example.mallproduct.entity.AttrGroupEntity;
import com.example.mallproduct.service.AttrGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author charlie
 * @email @gmail.com
 * @date 2023-11-26 14:55:58
 */
@RestController
@RequestMapping("mallproduct/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.catelogIdByGroupId(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @RequestMapping("/info-catelogId")
    public R infoByCatelogId(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = attrGroupService.infoByCatelogId(params);

        return R.ok().put("data", pageUtils);
    }

    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> list = attrGroupService.getAttrRelation(attrGroupId);

        return R.ok().put("data", list);
    }

    /**
     * 批量删除关联关系
     *
     * @param attrGroupEntityList
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody List<AttrGroupEntity> attrGroupEntityList) {
        attrGroupService.relationDelete(attrGroupEntityList);

        return R.ok();
    }

    /**
     * 解除关联
     * @param attrGroupId
     * @param params
     * @return
     */
    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R noAttrRelation(@PathVariable("attrGroupId") Long attrGroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.getNoAttrRelation(params, attrGroupId);

        return R.ok().put("page", page);
    }

    @RequestMapping("/attr/relation")
    public R saveBatchAttrRelation(@RequestBody List<AttrGroupEntity> attrGroupEntityList) {
        attrGroupService.saveBatchAttrRelation(attrGroupEntityList);

        return R.ok();
    }

    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrGroupWithAttrs(catelogId);

        return R.ok().put("data", attrGroupWithAttrsVos);
    }

}
